<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\AdminUsersRelatedModel;
use App\Models\TaskUserModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Illuminate\Support\Facades\Auth;

class TaskUserController extends AdminController
{
    public function title(): string
    {
        return '我的任务';
    }

    public function grid(): NGrid
    {
        $grid = new NGrid(new TaskUserModel);

        $grid->model()->leftJoin('task', 'task_user.task_id', 'task.id')
            ->leftJoin('admin_users', 'admin_users.id', 'creator_id')
            ->leftJoin('admin_users as follow_users', 'follow_users.id', 'task_user.follow_user_id')
            ->select([
                'task_user.*',
                'task_name',
                'admin_users.name',
                'task.remark',
                'follow_users.name as follow_user_name'
            ])->orderByDesc('task_user.id');
//        if (!ISADMIN) {
//            if (ISMAIN) $grid->model()->where('task_user.main_id', USERID);
//            else $grid->model()->where('task_user.follow_user_id', USERID);
//        }
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('task_user.main_id', MAINID);
            else $grid->model()->where('task_user.follow_user_id', USERID);
        }
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('task-task_name', '任务名');
                $filter->equal('task_user-follow_user_id', '回访人')->select((new AdminUsersRelatedModel)->stuff());
            });
        });
        if (Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('task_name', __('task.taskName'))->display(function () {
            if ($this->total_task_num > 0) return "<a href='/admin/taskUser/related/" . $this->id . "' style='color: black;'>$this->task_name</a>";
            else return $this->task_name;
        });
        $grid->column('follow_user_name', '回访人');
        $grid->column('remark', __('l.remark'));
        $grid->column('intentional_num', __('task.intentionalNum'))->display(function () {
            if ($this->intentional_num > 0) return '<a style="color: #000000;" href="/admin/taskUser/related/' . $this->id . '?&task_user_related-status=2">' . $this->intentional_num . '</a>';
            else return 0;
        });
        $grid->column('none_reply_num', __('task.noneReplyNum'))->display(function () {
            if ($this->none_reply_num > 0) return '<a style="color: #000000;"href="/admin/taskUser/related/' . $this->id . '?&task_user_related-status=0">' . $this->none_reply_num . '</a>';
            else return 0;
        });
        $grid->column('hang_up_num', __('task.hangUpNum'))->display(function () {
            if ($this->hang_up_num > 0) return '<a style="color: #000000;" href="/admin/taskUser/related/' . $this->id . '?&task_user_related-status=1">' . $this->hang_up_num . '</a>';
            else return 0;
        });
        $grid->column('invalid_num', __('task.invalidNum'))->display(function () {
            if ($this->invalid_num > 0) return '<a style="color: #000000;"href="/admin/taskUser/related/' . $this->id . '?&task_user_related-status=3">' . $this->invalid_num . '</a>';
            else return 0;
        });
        $grid->column('blacklist_num', __('task.blacklistNum'))->display(function () {
            if ($this->invalid_num > 0) return '<a style="color: #000000;" href="/admin/taskUser/related/' . $this->id . '?&task_user_related-status=4">' . $this->invalid_num . '</a>';
            else return 0;
        });
        $grid->column('completion_progress_num', __('task.completionProgressNum'));
        $grid->column('total_task_num', __('task.totalTaskNum'));
        $grid->column('name', __('task.creator'));
        $grid->column('created_at', __('l.createdAt'))->display(function ($date) {
            return date('Y-m-d H:i:s', strtotime($date));
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableView();
            $action->disableDelete();
//                if($action->row->total_task_num > 0) $action->append(new TaskRelatedTool($action->row->task_id));
        });

        return $grid;
    }
}
